<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>算法学习</title>
</head>

<body>

</body>

</html>
<script>
    // 一、两数之和: 因为nums[0]+nums[1]=2+7=9,所以返回[0,1]
    var arr = [2, 7, 11, 15]
    var target = 20
    function sumIdex(arr, target) {
        for (var i = 0; i < arr.length; i++) {
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == target) {
                    return [i, j]
                }else{
                    return '找不到' 
                }
            }
        }
    }
    console.log(sumIdex(arr, target))

    /**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
    var twoSum = function (nums, target) {
        let i = nums.length;
        while (i > 1) {
            let last = nums.pop();
            if (nums.indexOf(target - last) > -1) {
                return [nums.indexOf(target - last), nums.length]
            }
            i--
        }
    };
    console.log(twoSum(arr, target))
</script>